package dwz.web;

//import javax.inject.Inject;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import dwz.business.book.Book;
import dwz.business.book.BookServiceMgr;
import dwz.business.book.Chapter;
import dwz.business.book.ChapterConditionVO;
import dwz.business.lotto.DaiLiHuiyuanInterface;
import dwz.business.lotto.YongHuInterface;
import dwz.common.util.MD5Util;
import dwz.persistence.beans.ChongFuDengLu;
import dwz.persistence.beans.ChongFuDengLuExample;
import dwz.persistence.beans.DaiLiHuiyuan;
import dwz.persistence.beans.DaiLiHuiyuanExample;
import dwz.persistence.beans.YongHu;
import dwz.persistence.beans.YongHuExample;
import dwz.persistence.mapper.ChongFuDengLuMapper;
import dwz.web.management.lotto.CommonFields;

@Controller
public class IndexController extends BaseController{


	
	@Autowired
	private DaiLiHuiyuanInterface daiLiHuiyuanInterface;
	@Autowired
	YongHuInterface yongHuInterface;
	@Autowired
	ChongFuDengLuMapper chongFuDengLuMapper;
	
	
	
	@RequestMapping("front")
	public String index(HttpServletRequest request) {
		return "/management/front/login";
	}
	
	
	/**
	 * 登录 验证
	 * @return
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request){
		String url = "/management/front/login";
		request.getSession().invalidate();
		try {
			String userName = request.getParameter("admin_username390946370");
			String password = request.getParameter("admin_password390946370");
			YongHuExample yongHuExample = new YongHuExample();
			yongHuExample.createCriteria().andYhm_jEqualTo(userName);
			if(userName != null){
				YongHu yongHu = yongHuInterface.selectEntityByExample(yongHuExample);
				if(yongHu !=null && yongHu.getMm_j().equals(MD5Util.MD5(password))){
					url = "/management/front/frame";
					request.getSession().setAttribute(CommonFields.USERID, yongHu.getId());
					request.getSession().setAttribute(CommonFields.ISMODIFYPASSWORD, yongHu.getSfxgg_j());
					request.getSession().setAttribute(CommonFields.XINYONG, yongHu.getF7());
					buildLoginInfo(request, yongHu.getId(),CommonFields.FRONTFLAG);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "/management/front/login";
		}
		return url;
	}

	/**
	 * 插入登录的信息
	 * @param request
	 * @param yongHu
	 */
	private void buildLoginInfo(HttpServletRequest request, Integer userId,String flag) {
		ChongFuDengLuExample chongFuDengLuExample = new ChongFuDengLuExample();
		chongFuDengLuExample.createCriteria().andBFEqualTo(flag).andUserIdEqualTo(userId);
		chongFuDengLuMapper.deleteByExample(chongFuDengLuExample);
		
		//添加当前的的信息
		ChongFuDengLu chongFuDengLu = new ChongFuDengLu();
		chongFuDengLu.setBF(flag);
		chongFuDengLu.setSessionId(request.getSession().getId());
		chongFuDengLu.setUserId(userId);
		
		chongFuDengLuMapper.insertSelective(chongFuDengLu);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	@RequestMapping("/back")
	public String back(){
		return "/management/back/login";
	}
	
	/**
	 * 退出
	 * @return
	 */
	@RequestMapping("/tuichu")
	public String tuichu(HttpServletRequest request){
		request.getSession().setAttribute(CommonFields.USERID, null);
		request.getSession().setAttribute(CommonFields.ISMODIFYPASSWORD, null);
		request.getSession().invalidate();
		return "redirect:/back";
	}
	
	
	@RequestMapping("/backlogin")
	public String backlogin(HttpServletRequest request) {
		request.getSession().invalidate();
		String username = request.getParameter("admin_username699712556");
		String password = request.getParameter("admin_password699712556");
		DaiLiHuiyuanExample example = new DaiLiHuiyuanExample();
		example.createCriteria().andYhm_jEqualTo(username);
		DaiLiHuiyuan daiLiHuiyuan = daiLiHuiyuanInterface.selectEntityByExample(example);
		String forwardUrl = "/management/back/frame";
		if(daiLiHuiyuan == null){
			forwardUrl =  "/management/back/login";
		}else {
			String md5Password = daiLiHuiyuan.getMm_j();
			if(!md5Password.equals(MD5Util.MD5(password))){
				forwardUrl =  "/management/back/login";
			}else {
				request.getSession().setAttribute(CommonFields.USERID, daiLiHuiyuan.getId());
				request.getSession().setAttribute(CommonFields.ISMODIFYPASSWORD, daiLiHuiyuan.getSfxgmm_j());
				buildLoginInfo(request, daiLiHuiyuan.getId(),CommonFields.BACKFLAG);
			}
		}
		return forwardUrl;
	}
	
	
	
	@RequestMapping("/top")
	public String top() {
		return "/management/front/top";
	}

}